package com.money.tracker.common.entity.rsp;

import com.money.tracker.common.enums.IResponseEnum;
import lombok.Builder;
import lombok.Data;

/**
 * <p>
 * 封装返回体
 * <p>
 *
 * @author zhangshuai
 * &#064;date  2024/09/21
 */
@Data
@Builder
public class IResponse<T> {
    private Integer code;

    private String msg;

    private T data;


    public static <T> IResponse<T> success() {
        return buildResponse(null, IResponseEnum.SUCCESS);
    }

    public static <T> IResponse<T> success(T data) {
        return buildResponse(data, IResponseEnum.SUCCESS);
    }

    public static <T> IResponse<T> fail() {
        return buildResponse(null, IResponseEnum.FAIL);
    }

    public static <T> IResponse<T> fail(T data) {
        return buildResponse(data, IResponseEnum.FAIL);
    }

    public static <T> IResponse<T> fail(T data, IResponseEnum iResponseEnum) {
        return buildResponse(data, IResponseEnum.FAIL);
    }

    public static <T> IResponse<T> fail(IResponseEnum iResponseEnum) {
        return buildResponse(null, iResponseEnum);
    }

    public static <T> IResponse<T> fail(String msg) {
        return IResponse.<T>builder()
                .code(IResponseEnum.FAIL.getCode())
                .msg(msg)
                .build();
    }

    private static <T> IResponse<T> buildResponse(T data, IResponseEnum iResponseEnum) {
        return IResponse.<T>builder()
                .code(iResponseEnum.getCode())
                .msg(iResponseEnum.getMsg())
                .data(data)
                .build();
    }
}
